﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tuwien.auto.eibclient;

namespace KNXFramework.KNXObjects.ValueObjects
{
    public class DimmingObjectValue : StepDirObjectValue
    {

        public DimmingObjectValue(int dir, int step) : base(dir, step)
        {
        }
        public DimmingObjectValue(EIBBuffer buffer) : base(buffer)
        {
        }
        public DimmingObjectValue(string value)
        {
            string[] splittedValues = value.Split(':');
            if (splittedValues.Length > 0)
            {
                StepCode = 1;
                string dir = splittedValues[0].ToLower();
                if (splittedValues.Length == 2)
                {
                    StepCode = Convert.ToInt32(splittedValues[1]);
                    if (StepCode < 1 || StepCode > 7)
                        throw new ApplicationException("DimmingObjectValue: Invalid step (must be between 1 and 7. Val : " + value);

                }


                if (dir == "stop")
                {
                    StepCode = 0;
                    Direction = 0;
                }
                else if (dir == "up")
                    Direction = 1;
                else if (dir == "down")
                    Direction = 0;
                else
                    throw new ApplicationException(string.Format("DimmingObjectValue: Bad value: '{0}'", value));

            }
            
        }

        public override string ToString()
        {
            if (StepCode == 0)
                return "stop";

            string ret = "{0}:0{1}";
            return string.Format(ret, Direction == 1 ? "up" : "down", StepCode);

        }
    }
}
